package cn.toma.core;

import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @Author: toma
 * @Date: 2019-03-18 10:11
 * @Description:
 */
public abstract class AbstractService<T> implements Service<T>
{

	@Autowired
	public Mapper<T> mapper;

	/**
	 * 当前泛型真实类型的Class
	 */
	private Class<T> modelClass;

	public AbstractService()
	{
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		modelClass = (Class<T>) pt.getActualTypeArguments()[0];
	}

	@Override
	public void save(T model)
	{
		mapper.insertSelective(model);
	}

	@Override
	public void save(List<T> models)
	{
		mapper.insertList(models);
	}

	@Override
	public void deleteById(Integer id)
	{
		mapper.deleteByPrimaryKey(id);
	}

	@Override
	public void deleteByIds(String ids)
	{
		mapper.deleteByIds(ids);
	}

	@Override
	public void update(T model)
	{
		mapper.updateByPrimaryKeySelective(model);
	}

	@Override
	public T findById(Integer id)
	{
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<T> find(T model)
	{
		return mapper.select(model);
	}

	@Override
	public T findBy(T model)
	{
		return mapper.selectOne(model);
	}

	@Override
	public List<T> findByIds(String ids)
	{
		return mapper.selectByIds(ids);
	}

	@Override
	public List<T> findByCondition(Condition condition)
	{
		return mapper.selectByCondition(condition);
	}

	@Override
	public List<T> findAll()
	{
		return mapper.selectAll();
	}

	@Override
	public int findCount(T model)
	{
		return mapper.selectCount(model);
	}
}
